import select

from log import Log

log = Log(tag='TASK')


class Task:
    def setup(self):
        pass

    def loop(self):
        return True

    def __call__(self):
        return self.loop()


class PollTask(Task):
    def __init__(self):
        self.__poll_timeout = 10
        self.__poll = None

    def setup(self):
        self.__poll = select.poll()
        self.load_resource(self.__poll)

    def loop(self):
        try:
            results = self.__poll.poll(self.__poll_timeout)
            self.on_poll_result(results)
        except Exception as e:
            log.warn('Error on poll', e)
        #  raise e
        return True

    def on_poll_result(self, results):
        pass

    def load_resource(self, poll):
        pass
